#coding:utf-8
import requests
import json
import time
import datetime

#保存成json文件
def store(data, i):
    with open('data' + str(i) +'.json', 'a+') as json_file:
        json_file.write(json.dumps(data,ensure_ascii=True,indent=4))


#找到最后一个字符
def find_last(string,str):
    last_position=-1
    while True:
        position=string.find(str,last_position+1)
        if position==-1:
            return last_position
        last_position=position

#创建总揽url
def createRequestUrl(pagenum, category, status, limit, time):
    firsturl = "http://www.vpgame.com/gateway/v1/match/?callback=" #访问固定url地址
    queryurl = "jQuery19104807921946210463_" + time               #查询id + 时间
    page = "&page=" + pagenum                                     #查询分页
    category = "&category=" + category                            #查询类型 dota csgo all
    status = "&status=" + status
    limit = "&limit=" + limit                                     #每页限制
    chart = "&lang=zh_CN&_=" + time                               #查询时间
    url = firsturl + queryurl + page + category + status + limit + chart #查询url拼接
    return url

#创建详细url
def createDetailUrl(tid, time):
    firsturl = "http://www.vpgame.com/gateway/v1/match/schedule?callback="  # 访问固定url地址
    queryurl = "jQuery19104807921946210463_" + time  # 查询id + 时间
    tidurl = "&tid=" + tid;
    chart = "&lang=zh_CN&_=" + time                #查询时间
    url = firsturl + queryurl + tidurl + chart  # 查询url拼接
    return url

now = int(time.time() * 1000) #获取当前时间
page = 1                 #查询页
max_page = 200;          #最大访问页数

# 解决js中文转义
#str1 = "\u8c01\u5c06\u8d62\u5f97\u6b64\u7cfb\u5217\u8d5b\u7684\u80dc\u5229?"
#print str1.decode('unicode-escape').encode('utf-8')
#参考url格式
#mainUrl = "http://www.vpgame.com/gateway/v1/match/?callback=jQuery19107315240364441622_1505607652537" \
   #   "&page=1&category=dota&status=close&limit=6&lang=zh_CN&_=1505607652540"

#subUrl = "http://www.vpgame.com/gateway/v1/match/schedule?callback=jQuery19105570593197642995_505607652537" \
     #  "&tid=" + n['tournament_schedule_id'] + "&lang=zh_CN&_=11505607652540"

#http://www.vpgame.com/gateway/v1/match/schedule/list?callback=jQuery19104807921946210463_1505631160995&page=1\
#&limit=11&lang=zh_CN&_=1505631160996

#http://www.vpgame.com/gateway/v1/match/?callback=jQuery19104807921946210463_1505631160993&tid=100103338&lang=zh_CN&_=1505631160993
#http://www.vpgame.com/gateway/v1/match/schedule?callback=jQuery19104807921946210463_1505631160993&tid=100103338&lang=zh_CN&_=1505631161000

for num in range (page, page + max_page):
    #查询dota请求url
    url = createRequestUrl(str(num), "dota", "close", "50", str(now))
    #print url
    data = requests.get(url)
    information = data.content
    if (information is []):
        break

    #找到joson数据起始点
    begin = information.index('{')
    #找到joson数据中指点
    end = find_last(information, '}')

    #加载json数据
    wdata = json.loads(information[begin:end + 1])
    news = wdata['body']
    jsonfile = []
    m = 0
    for n in news:
        m += 1
        suburl = createDetailUrl(str(n['tournament_schedule_id']), str(now))
        print suburl
        subdata = requests.get(suburl)

        subinformation = subdata.content
        subbegin = subinformation.index('{')
        subbeginend = find_last(subinformation, '}')
        subinfo = subinformation[subbegin:subbeginend + 1].decode('unicode-escape')
        subwdata = json.loads(subinfo)


        new2 = subwdata['body']
        #print information2
        jsonData = []
        for n2 in new2:
            #n2['status_name'] = n2['status_name'].decode('unicode-escape')
            #n2['mode_name'] = n2['mode_name'].decode('unicode-escape')
            #data3 = {'id': n['id'],
               #     'mode':n2['mode'],
              #      'left_team' :n2['left_team'],
              #      'right_team':n2['right_team'],
              #      'right_odd':n2['right_odd'],
               #     'game_time':n2['game_time'],
               #     'date':n2['date']}
            jsonData.append(n2)
            #print data3
        jsonfile.append(jsonData)

    #保存
    store(jsonfile, num)





